package org.dtrd.modules.food.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dtrd.common.system.vo.LoginUser;
import org.dtrd.modules.food.entity.bean.EmsGlycemicIndexBean;
import org.dtrd.modules.food.entity.bean.FoodDetail;
import org.dtrd.modules.food.entity.bean.FoodListInfo;
import org.dtrd.modules.food.entity.po.DtrdExtEmsYwFoods;
import org.dtrd.modules.food.entity.request.FoodPageRequest;
import org.dtrd.modules.food.mapper.DtrdExtEmsYwFoodsMapper;
import org.dtrd.modules.food.service.IDtrdExtEmsYwFoodsService;
import org.dtrd.modules.util.ShiroUtil;
import org.dtrd.common.util.StringUtil;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;


/**
 * <p>
 * 食材库 服务实现类
 * </p>
 *
 * @author QR
 * @since 2021-12-08
 */
@DS("multi-datasource1")
@Service
public class DtrdExtEmsYwFoodsServiceImpl extends ServiceImpl<DtrdExtEmsYwFoodsMapper, DtrdExtEmsYwFoods> implements IDtrdExtEmsYwFoodsService {

    private DtrdExtEmsYwFoodsMapper dtrdExtEmsYwFoodsMapper;

    @Override
    public IPage<EmsGlycemicIndexBean> selectGiFoodList(Integer giIndex, String foodName, Integer pageNum, Integer pageSize) {
        IPage<EmsGlycemicIndexBean> page = new Page<>(pageNum, pageSize);
        return baseMapper.selectGiFoodList(page,giIndex, foodName);
    }

    @Override
    public List<FoodDetail> getFoodsList(FoodPageRequest request) {
        LambdaQueryWrapper<DtrdExtEmsYwFoods> queryWrapper = baseQueryWrapper();
        List<DtrdExtEmsYwFoods> poList = list(queryWrapper);
        List<FoodDetail> dtoList = new ArrayList<>(poList.size());
        if (!poList.isEmpty()) {
            dtoList = poList.stream()
                .map(po -> new FoodDetail().parseFromPo(po))
                .collect(Collectors.toList());
        }
        return dtoList;
    }

    @Override
    public FoodDetail getFoods(Integer dataId) {
        DtrdExtEmsYwFoods dtrdExtEmsYwFoods = getById(dataId);
        return Optional.ofNullable(dtrdExtEmsYwFoods)
                        .map(po -> new FoodDetail().parseFromPo(po))
                        .orElse(null);
    }

    @Override
    public boolean saveOrUpdateFoods(FoodDetail dto) {
        DtrdExtEmsYwFoods dtrdExtEmsYwFoods = new DtrdExtEmsYwFoods();
        LoginUser loginUser = ShiroUtil.getLoginUser();
        if(dto.getDataId() == null){
            dtrdExtEmsYwFoods = dtrdExtEmsYwFoods.init();
            if (loginUser != null) {
                dtrdExtEmsYwFoods.setFoodCreater(loginUser.getId());
            }
        }else {
            if (loginUser != null) {
                dtrdExtEmsYwFoods.setFoodEditor(loginUser.getId());
            }
        }
        dtrdExtEmsYwFoods.parseFromDto(dto);
        return saveOrUpdate(dtrdExtEmsYwFoods);
    }

    @Override
    public boolean removeFoods(Integer dataId) {
        DtrdExtEmsYwFoods query = this.getById(dataId);
        LoginUser loginUser = ShiroUtil.getLoginUser();
        if (loginUser != null) {
            query.setFoodDeleter(loginUser.getId());
        }
        query.setFoodDelTime(new Date());
        query.setIsDel(1);
        return updateById(query);
    }

    @Override
    public IPage<FoodListInfo> pageFood(FoodPageRequest request) {
        LambdaQueryWrapper<DtrdExtEmsYwFoods> queryWrapper = baseQueryWrapper()
                .orderByDesc(DtrdExtEmsYwFoods::getFoodCreateTime);
        String foodName = request.getFoodName();
        Integer foodKcal = request.getFoodKcal();
        Integer foodCategoryId = request.getFoodCategoryId();
        if (foodName != null) {
            queryWrapper.like(DtrdExtEmsYwFoods::getFoodName, StringUtil.fuzzySearchWrapper(foodName));
        }
        if (foodKcal != null) {
            queryWrapper.le(DtrdExtEmsYwFoods::getFoodKcal, foodKcal);
        }
        if (foodCategoryId != null) {
            queryWrapper.eq(DtrdExtEmsYwFoods::getFoodCategoryid, foodCategoryId);
        }
        Page<DtrdExtEmsYwFoods> page = new Page<>(request.getPageNum(), request.getPageSize());
        page = this.page(page, queryWrapper);
        return page.convert(FoodListInfo::new);
    }

    private LambdaQueryWrapper<DtrdExtEmsYwFoods> baseQueryWrapper() {
        return Wrappers.lambdaQuery(DtrdExtEmsYwFoods.class)
                        .eq(DtrdExtEmsYwFoods::getIsDel, 0);
    }

    private LambdaUpdateWrapper<DtrdExtEmsYwFoods> baseUpdateWrapper() {
        return Wrappers.lambdaUpdate(DtrdExtEmsYwFoods.class)
                        .eq(DtrdExtEmsYwFoods::getIsDel, 0);
    }
}
